<?php
/**
 * 附件上传与管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
UE_Loader::loadModel('UploaderModel', 'system');
class UploaderController extends UE_Controller_Action {
	/**
	 * @var UploaderModel
	 */
	public $uploader;
	
	public function init() {
		$this->uploader = new UploaderModel();
		$this->uploader->setField('file');
	}
	
	public function addAction() {
		if (IS_POST && $_FILES) {
			//print_r($_FILES);			
			empty($_REQUEST['no_watermark']) || $this->uploader->imgWatermark = false;
						
			$r = $this->uploader->add(array());
			if (!empty($_GET['ajax'])) {
				jsHeader();
				if ($r) {
					print json_encode(array('err' => '', 'title' =>$r['upload_name'], 'thumb' => $r['upload_path'], 'img' => $r['upload_path']));
				} else {
					print json_encode(array('err' => $this->uploader->getErrs()));
				}
				
				return ;
			} else {				
				if ($r) {
					$this->tpl->setOK('成功上传文件');
				} else {
					$this->tpl->setErr($this->uploader->getErrs());
				}
			}
		}
		
	    $this->tpl->apply('system/uploader.add');
	}
	
	public function loadAction() {
	    print_r($GLOBALS);
	}
	
	public function thumbAction() {
		$this->id = (int)$this->id;
	    if (!$this->id) {
	    	print 'Error Param!';
	    	return ;
	    }
	    
	    $width = empty($_GET['width']) ? 100 : $_GET['width'];
	    $height = empty($_GET['height']) ? 100 : $_GET['height'];
	    
	    print thumb($this->id, $width, $height);	    
	}
	
	public function delAction() {
	    
	}	
	
	
	public function listAction() {		
		jsHeader();
		
		$filter = '';
		// 自己上传的图片
		if ($this->id == 'my' || $_SESSION['user_type'] == 'member') {
			$filter .= " AND user_id = '{$_SESSION['user_id']}'";
		}
	    
	    $images = array();
	    $images['err'] = '';
	    $images['pages'] = 0;  // 总页数
	    
	    $sql = "SELECT COUNT(*) FROM ue_upload WHERE upload_is_image=1 $filter";	    
	    $images['totals'] = UE_DB::factory()->getfirstField($sql);
	    
	    if ($images['totals']) {	    	
		    $paging = new UE_Paging();
		    $paging->setVar($images['totals'], 24);		    
		    $images['pages'] = $paging->lastPage;  
		    $rows = UE_DB::factory()->getAll("SELECT upload_path, upload_name, upload_id FROM ue_upload WHERE upload_is_image=1 $filter ORDER BY upload_id DESC LIMIT {$paging->firstCount}, {$paging->pageNo}");
		    foreach ($rows as $r) {
		    	$images['imgs'][] = array(
		    	    'img'   => $r['upload_path'],
		    	    'title' => $r['upload_name'],
		    	    'thumb' => thumb($r['upload_id'], 68, 68),
		    	);
		    }
	    }
	    
	    print json_encode($images);
	}	
}